package com.mzj.saas.mservice.house.controller;

import com.mzj.saas.commons.HouseApiData;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.house.service.HouseService;
import com.mzj.saas.mservice.house.vo.BatchHouseAuditVO;
import com.mzj.saas.mservice.house.vo.HouseInfoVO;
import com.mzj.saas.mservice.house.vo.HouseVO;
import com.mzj.saas.mservice.house.vo.OrgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公租房接口
 */
@Controller
@RequestMapping("/house")
@Api(tags = "房屋管理")
public class HouseController {
    @Autowired
    private HouseService houseService;

    /**
     * 公租房列表
     */
    /**
     * 公租房列表,查询公租房列表,实现了已录入的公租房列表的显示和公租房的搜索，可以根据公租房的所有字段进行搜索，从而找出对应的公租房信息
     * @param accessToken 权限鉴定
     * @param pageNumber  当前页
     * @param pageSize  页数
     * @param residenceCommunityId 小区id
     * @param buildingNum 楼栋
     * @param unit 单元
     * @param roomNumber 房号
     * @param districtId 区
     * @param bindStatus 产权人绑定状态:0未绑定 1绑定
     * @param matchStatus 配租状态：0未配租 1可配租 2已配租
     * @param leaseStatus 出租状态：0未出租 1已出租
     * @param pwdLockStatus 密码锁授权：0未授权 1已授权 4已冻结
     * @param houseNature 性质:1公租房、2廉租房、3商品房、4经济适用房、5其它
     * @param checkStatus 核查状态：0未核查、1已核查
     * @param orgId 	组织id
     * @param search 搜索条件
     * @param houseId 房屋id
     * @param floor 	楼层
     * @param area 	面积
     * @param decorateType 装修
     * @param provinceId 	省
     * @param cityId 市
     * @param streetId 街道
     * @param communityId 社区
     * @param address 地址
     * @param auditStatus 	房源审核状态:0未审核 1已审核
     * @param room 室
     * @param hall 	厅
     * @param toilet 卫
     * @param houseIdList 房屋id集合
     * @return
     */
    @GetMapping("/houseList")
    @ResponseBody
    public ResultBean<HouseApiData<HouseVO>> houseList(@RequestHeader String accessToken,
                                                       @RequestParam(defaultValue = "0") Integer pageNumber,
                                                       @RequestParam(defaultValue = "5") Integer pageSize,
                                                       @RequestParam(required = false) String residenceCommunityId,
                                                       @RequestParam(required = false) Long buildingNum,
                                                       @RequestParam(required = false) Long unit,
                                                       @RequestParam(required = false) String roomNumber,
                                                       @RequestParam(required = false) Long districtId,
                                                       @RequestParam(required = false) Integer bindStatus,
                                                       @RequestParam(required = false) Integer matchStatus,
                                                       @RequestParam(required = false) Integer leaseStatus,
                                                       @RequestParam(required = false) Integer pwdLockStatus,
                                                       @RequestParam(required = false) Integer houseNature,
                                                       @RequestParam(required = false) Integer checkStatus,
                                                       @RequestParam(required = false) Long orgId,
                                                       @RequestParam(required = false) String search,
                                                       @RequestParam(required = false) String houseId,
                                                       @RequestParam(required = false) Integer floor,
                                                       @RequestParam(required = false) BigDecimal area,
                                                       @RequestParam(required = false) Integer decorateType,
                                                       @RequestParam(required = false) Long provinceId,
                                                       @RequestParam(required = false) Long cityId,
                                                       @RequestParam(required = false) Long streetId,
                                                       @RequestParam(required = false) Long communityId,
                                                       @RequestParam(required = false) String address,
                                                       @RequestParam(required = false) Integer auditStatus,
                                                       @RequestParam(required = false) Integer room,
                                                       @RequestParam(required = false) Integer hall,
                                                       @RequestParam(required = false) Integer toilet,
                                                       @RequestParam(required = false) List<String> houseIdList) {
        return houseService.houseList(accessToken, pageNumber, pageSize, residenceCommunityId, buildingNum, unit, roomNumber,
                districtId, bindStatus, matchStatus, leaseStatus, pwdLockStatus, houseNature, checkStatus, orgId, search,
                houseId, floor, area, decorateType, provinceId, cityId, streetId, communityId, address, auditStatus,room,hall,toilet,houseIdList);
    }

    /**
     * 产权所有人下拉框
     */
    @PostMapping("/findOrgId")
    @ResponseBody
    public ResultBean<List<OrgVO>> findOrgId(@RequestHeader String accessToken,
                                             @RequestBody List<Long> orgIds) {
        return houseService.findOrgId(accessToken, orgIds);
    }

    /**
     * 用户所属产权单位
     */
    @GetMapping("/findUserOrg")
    @ResponseBody
    public ResultBean<OrgVO> findUserOrg(@RequestHeader String accessToken) {
        return houseService.findUserOrg(accessToken);
    }

    /**
     * 新增公租房
     */
    /**
     * 新增公租房,实现了公租房房源的录入
     * @param accessToken 权限鉴定
     * @param vo 一个用来接收前端传过来的数据的实体类
     * @return
     */
    @PostMapping("/saveHouse")
    @ResponseBody
    public ResultBean<Object> saveHouse(@RequestHeader String accessToken, @RequestBody HouseVO vo) {
        return houseService.saveHouse(accessToken, vo);
    }

    /**
     * 修改公租房
     */
    /**
     * 修改公租房,实现了未绑定公租房的修改
     * @param accessToken 权限鉴定
     * @param houseId 公租房ID
     * @param vo 公租房实体类
     * @return
     */
    @PostMapping("/updateHouse")
    @ResponseBody
    public ResultBean<Object> updateHouse(@RequestHeader String accessToken, @RequestParam(value = "houseId") String houseId, @RequestBody HouseVO vo) {
        vo.setId(houseId);
        return houseService.updateHouse(accessToken, vo);
    }

    /**
     * 公租房详情
     */
    @GetMapping("/findHouse")
    @ResponseBody
    public ResultBean<HouseInfoVO> findHouse(@RequestParam(value = "houseId") String houseId) {
        return houseService.findHouse(houseId);
    }

    /**
     * 删除公租房
     */
    /**
     * 删除公租房,实现了未审核的公租房的删除
     * @param houseId 公租房ID
     * @return
     */
    @GetMapping("/deleteHouse")
    @ResponseBody
    public ResultBean<Object> deleteHouse(@RequestParam(value = "houseId") String houseId) {
        return houseService.deleteHouse(houseId);
    }

    /**
     * 查询绑定产权所有人
     */
    @GetMapping("/findOwner")
    @ResponseBody
    public ResultBean<Map<String, Object>> findOwner(@RequestParam(value = "houseId") String houseId) {
        return houseService.findOwner(houseId);
    }

    /**
     * 产权人列表
     */
    /**
     * 产权人列表,实现了查询出所有产权单位所有人数据
     * @param accessToken 权限鉴定
     * @param name 用户名字
     * @return
     */
    @GetMapping("/ownerList")
    @ResponseBody
    public ResultBean<List<OrgVO>> ownerList(@RequestHeader String accessToken,
                                             @RequestParam(value = "name", required = false) String name) {
        return houseService.ownerList(accessToken, name);
    }

    /**
     * 所属物业列表
     */
    /**
     * 所属物业列表,实现了查询出所有物业列表所有人数据
     * @param accessToken 权限鉴定
     * @param name 用户名字
     * @return
     */
    @GetMapping("/propertyList")
    @ResponseBody
    public ResultBean<List<OrgVO>> propertyList(@RequestHeader String accessToken,
                                                @RequestParam(value = "name", required = false) String name) {
        return houseService.propertyList(accessToken, name);
    }


    /**
     * 绑定产权所有人
     */
    /**
     * 绑定产权所有人,实现绑定产权所有人
     * @param accessToken 权限鉴定
     * @param houseIds 公租房ID
     * @param orgId 产权人ID
     * @return
     */
    @GetMapping("/bindOwner")
    @ResponseBody
    public ResultBean<Object> bindOwner(@RequestHeader String accessToken,
                                        @RequestParam(value = "houseIds") List<String> houseIds,
                                        @RequestParam(value = "orgId") Long orgId) {
        return houseService.bindOwner(accessToken, houseIds, orgId);
    }

    /**
     * 修改产权所有人
     */
    @GetMapping("/updateBindOwner")
    @ResponseBody
    public ResultBean<Object> updateBindOwner(@RequestHeader String accessToken,
                                              @RequestParam(value = "houseId") String houseId,
                                              @RequestParam(value = "orgId") Long orgId) {
        return houseService.updateBindOwner(accessToken, houseId, orgId);
    }

    /**
     * 解绑产权所有人
     */
    /**
     *  解绑产权所有人，实现了产权所有人的解绑
     * @param houseId 公租房ID
     * @return
     */
    @GetMapping("/unBindOwner")
    @ResponseBody
    public ResultBean<Object> unBindOwner(@RequestParam(value = "houseId") String houseId) {
        return houseService.unBindOwner(houseId);
    }

    /**
     * 设置装修情况
     */
    @GetMapping("/saveDecorate")
    @ResponseBody
    public ResultBean<Object> saveDecorate(@RequestParam(value = "houseId") String houseId,
                                           @RequestParam(required = true) Integer decorateType) {
        return houseService.saveDecorate(houseId, decorateType);
    }

    /**
     * 查询楼层数
     */
    @GetMapping("/findFloor")
    @ResponseBody
    public ResultBean<Object> findFloor(@RequestParam(value = "buildId") Long buildId) {
        return houseService.findFloor(buildId);
    }

    /**
     * 小区列表
     * <p>
     * communityId 社区id
     */
    @GetMapping("/communityList")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> communityList(@RequestParam(value = "communityId") Long communityId) {
        return houseService.communityList(communityId);
    }

    /**
     * 企业绑定产权单位审批状态
     *
     * @author shun
     * 2021年6月30日
     */
    @GetMapping("/org/audit")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> findEnterpriseAuditStatus(@RequestParam(value = "provinceId") Long provinceId,
                                                                           @RequestParam(value = "cityId") Long cityId,
                                                                           @RequestParam(value = "districtId") Long districtId,
                                                                           @RequestParam(value = "streetId") Long streetId,
                                                                           @RequestParam(value = "communityId") Long communityId) {
        houseService.findEnterpriseAuditStatus(provinceId, cityId, districtId, streetId, communityId);
        return null;
    }

    /**
     * 批量房源审核
     * @param vo 公租房实体类
     * @return
     */
    @PostMapping("/batchHouseAudit")
    @ResponseBody
    public ResultBean<Object> batchHouseAudit(@RequestBody BatchHouseAuditVO vo) {
        return houseService.batchHouseAudit(vo);
    }


    /******************************************/

    /**
     * 公租房-数据
     */
    /**
     * 公租房管理模块首页公租房-数据 实现了公租房管理模块首页数据展示
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/pageData")
    @ResponseBody
    public ResultBean<Map<String, Object>> pageData(@RequestHeader String accessToken) {

        return houseService.pageData(accessToken);
    }

    /**
     * 保障房-数据
     */
    /**
     * 公租房管理模块首页保障房-数据 实现了公租房管理模块首页数据展示
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/timePageData")
    @ResponseBody
    @ApiOperation("首页保障房-数据统计")
    public ResultBean<Map<String, Object>> timePageData(@RequestHeader String accessToken ) {

        return houseService.timePageData(accessToken);
    }

    /**
     * 房管理模块首页非公租房-数据 首页出租房-数据统计
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/rentalTimePageData")
    @ResponseBody
    @ApiOperation("首页出租房-数据统计")
    public ResultBean<Map<String, Object>> rentalTimePageData(@RequestHeader String accessToken ) {

        return houseService.rentalTimePageData(accessToken);
    }
    /**
     * 房管理模块首页其他房产-数据
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/allTimePageData")
    @ResponseBody
    @ApiOperation("首页其他房产-数据统计")
    public ResultBean<List<Map<String, Object>>> allTimePageData(@RequestHeader String accessToken ) {

        return houseService.allTimePageData(accessToken);
    }
    /**
     * 公租房-人员数据
     */
    /**
     * 公租房管理模块首页公租房-人员数据，实现了公租房人员数据和公租房人员已配租数据的展示
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/peopleData")
    @ResponseBody
    public ResultBean<Map<String, Object>> peopleData(@RequestHeader String accessToken) {

        return houseService.peopleData(accessToken);
    }

    /**
     * 开门记录数据  saas_smart_lock
     */
    /**
     * 开门记录数据，实现了90天未开门房屋户数数据的展示
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/lockData")
    @ResponseBody
    public ResultBean<Map<String, Object>> lockData(@RequestHeader String accessToken) {
        //创建一个map对象来存储数据
        Map<String, Object> map = new HashMap<>();
        //获取90天未开门房屋户数的数据
        List<Map<String, Object>> data = houseService.lockData(accessToken).getResultData();

        int num = 0;
        num = data.size();

        map.put("num", num);
        map.put("data", data);
        //把数据返回给前端
        return ResultBean.successfulResult(map);
    }

    /**
     * 公租房-缴费数据，[租金/押金]
     */
    /**
     * 公租房-缴费数据，[租金/押金]实现了公租房缴费数据的展示
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/payData")
    @ResponseBody
    public ResultBean<Object> payData(@RequestHeader String accessToken) {

        return houseService.payData(accessToken);
    }

    /**
     * 公租房-缴费数据详情，[租金/押金]
     * type :  1正常缴费数据, 2正常欠费数据, 3逾期欠费数据
     */
    /**
     * 公租房-缴费数据详情，[租金/押金],实现公租房缴费数据详情的查询
     * @param accessToken 权限鉴定
     * @param pageNumber 第条数据
     * @param pageSize  总共查询出多少条
     * @param type 标签
     * @return
     */
    @GetMapping("/payDetailsData")
    @ResponseBody
    public ResultBean<Object> payDetailsData(@RequestHeader String accessToken,
                                             @RequestParam(defaultValue = "0") Integer pageNumber,
                                             @RequestParam(defaultValue = "10") Integer pageSize,
                                             Integer type) {

        return houseService.payDetailsData(accessToken, pageNumber, pageSize, type);
    }

    /**
     * 公租房数据-导出
     * type : 1 公租房数据 , 2 公租房人员数据 , 3 公租房人员已配租数据
     */
    /**
     * 公租房数据-导出，用来导出公租房的数据，实现了不同公租房对应的数据导出
     * @param accessToken 权限鉴定
     * @param response  数据响应
     * @param type  标签
     * @throws Exception
     */
    @PostMapping("/pageDataExport")
    @ResponseBody
    public void pageDataExport(@RequestHeader String accessToken, HttpServletResponse response, Integer type) throws Exception {

        houseService.pageDataExport(accessToken, response, type);
    }

    /**
     * 开门记录数据-导出
     */
    /**
     * 开门记录数据-导出，实现了90天内未开门的承租人数据的导出
     * @param accessToken 权限鉴定
     * @param response 数据响应
     * @throws Exception
     */
    @GetMapping("/lockDataExport")
    @ResponseBody
    public void lockDataExport(@RequestHeader String accessToken, HttpServletResponse response) throws Exception {

        houseService.lockDataExport(accessToken, response);
    }

    /**
     * 公租房-缴费数据详情-导出
     * type :  1正常缴费数据, 2正常欠费数据, 3逾期欠费数据
     */
    /**
     * 公租房-缴费数据详情-导出,实现了公租房缴费数据详情的导出
     * @param accessToken 权限鉴定
     * @param pageNumber 条数
     * @param pageSize 总条数
     * @param type 标签
     * @param response 数据响应
     * @return
     * @throws Exception
     */
    @PostMapping("/payDetailsDataExport")
    @ResponseBody
    public ResultBean<Object> payDetailsDataExport(@RequestHeader String accessToken,
                                                   @RequestParam(defaultValue = "0") Integer pageNumber,
                                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                                   Integer type, HttpServletResponse response) throws Exception {

        return houseService.payDetailsDataExport(accessToken, response, pageNumber, pageSize, type);
    }
}
